home *** CD-ROM | disk | FTP | other *** search
/ PsL Monthly 1994 January / PSL Monthly Shareware CD-ROM (Public Software Library) (January 1994).iso / games / dos / misc / inqmind.com / INQMINDS.C < prev    next >
Encoding:
C/C++ Source or Header  |  1987-02-04  |  3.0 KB  |  156 lines

  1. /* ---------- "Enquiring minds want to know part 1" ---------- */
  2.  
  3. #include "stdio.h"
  4.  
  5. extern long time();
  6. extern short ran_vec[];
  7.  
  8. struct gstruct {
  9.          int items;
  10.          char *item[128];
  11.          char data;
  12.        } *group[64];
  13.  
  14. int groups;
  15. char line[2048];
  16. int startdata[10000];
  17. FILE *f1;
  18.  
  19. rnd(i)
  20.   int i;
  21. {
  22.   return((rand()&0xffff) % i);
  23. }
  24.  
  25. search(word)
  26.   char word[];
  27. {
  28.   int i;
  29.   for(i=0;i<groups;++i)
  30.   if(comps(word,group[i]->item[0]))return(i);
  31.   return(-1);
  32. }
  33.  
  34. comps(word1,word2)
  35.   char word1[],word2[];
  36. {
  37.   int i;
  38.   for(i=0;(word1[i]==word2[i]&&word1[i]!=0&&word2[i]!=0);++i);
  39.   if(word1[i]!=word2[i])return(0);
  40.   return(1);
  41. }
  42.  
  43. alpha(a)
  44.   char a;
  45. {
  46.   if((a>='a'&&a<='z')||(a>='A'&&a<='Z')||(a>='0'&&a<='9')||(a=='_'))return(1);
  47.   return(0);
  48. }
  49.  
  50. init(filename)
  51.   char filename[];
  52. {
  53.   int i;
  54.   char *pnt;
  55.   f1=fopen(filename,"r");
  56.   if(f1==NULL){printf("Bad open\n");exit();}
  57.   groups=0;
  58.   group[groups] = (struct gstruct *)(&startdata[0]);
  59.   group[groups]->items=0;
  60.   group[groups]->item[0]= &(group[groups]->data);
  61.   for(;;)
  62.     {
  63.       getline(f1,line);
  64.       if(line[0]=='%')
  65.         {
  66.           group[groups+1]=(struct gstruct *)((int)(group[groups]->item[group[groups]->items]+1) & -2);
  67.           ++groups;
  68.           group[groups]->items=0;
  69.           group[groups]->item[0]= &(group[groups]->data);
  70.           getline(f1,line);
  71.           if(line[0]=='%')return(1);
  72.         }
  73.       pnt=group[groups]->item[group[groups]->items];
  74.       for(i=0;(*(pnt++)=line[i++])!=0;);
  75.       group[groups]->items += 1;
  76.       group[groups]->item[group[groups]->items] = pnt;
  77.     }
  78. }
  79.  
  80. exec(word)
  81.   char word[];
  82. {
  83.   char *ppnt;
  84.   char word1[64];
  85.   int g;
  86.   g=search(word);
  87.   if(g == -1)
  88.     {
  89.       printf("%s",word);
  90.       return(1);
  91.     }
  92.   ppnt=group[g]->item[1+rnd(group[g]->items - 1)];
  93.   for(;;)
  94.     {
  95.       getword(&ppnt,word1);
  96.       if(word1[0]==0)return(1);
  97.       exec(word1);
  98.     }
  99. }
  100.  
  101. getword(ppnt,word)
  102.   char word[];
  103.   char **ppnt;
  104. {
  105.   int i;
  106.   word[0]=0;
  107.   while((!alpha(**ppnt))&&(**ppnt!=0))
  108.     {
  109.       if(**ppnt == '\\')printf("\n");
  110.       else if(**ppnt != '|')printf("%c",**ppnt);
  111.       (*ppnt)++;
  112.     }
  113.   if(**ppnt==0)return(1);
  114.   for(i=0;alpha(**ppnt);(*ppnt)++)word[i++]= **ppnt;
  115.   word[i]=0;
  116. }
  117.  
  118. getline(f1,line)
  119.   char line[];
  120.   FILE *f1;
  121. {
  122.   int i;
  123.   for(i=0;(line[i]=getc(f1))!='\n';++i);
  124.   line[i]=0;
  125. }
  126.  
  127. argerr()
  128.   printf("Correct usage is    code (-nn) protofile\n");
  129.   exit();
  130. }
  131.  
  132. main(argc,argv)
  133.   int argc;
  134.   char *argv[];
  135. {
  136.   int i,st,ncount;
  137.   char filename[32];
  138.   ncount=1;
  139.   if(argc==1) argerr();
  140.   if(argc>3) argerr();
  141.   if(argc==3)
  142.     {
  143.       ncount=0;
  144.       if(argv[1][0]!='-') argerr();
  145.       for(i=1;argv[1][i]!=0;++i)ncount=ncount*10+argv[1][i]-'0';
  146.       for(i=0;argv[2][i]!=0;++i)filename[i]=argv[2][i];
  147.     }
  148.   if(argc==2)
  149.     for(i=0;argv[1][i]!=0;++i)filename[i]=argv[1][i];
  150.   st = time(0);
  151.   srand(st);
  152.   init(filename);
  153.   for(i=0;i<ncount;++i)exec("CODE");
  154. }
  155.